package com.lepu.app.fun.my.adapter;

import java.io.File;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.app.utils.SdLocal;
import com.app.utils.Setting;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.DensityUtil;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.utils.secure.EncodeMD5;
import com.eyzhs.app.R;
import com.lepu.app.fun.my.bean.BeanPubContentImage;
import com.lepu.app.main.activity.AppImageBroswerActivity;

public class AdapterImage extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<BeanPubContentImage> mArrayList = null;

	public AdapterImage(Context context,
			ArrayList<BeanPubContentImage> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.my_pub_content_image_item, null);

			holder = new ViewHolder();

			ImageView itemImageView = (ImageView) convertView
					.findViewById(R.id.itemImageView);

			holder.itemImageView = itemImageView;
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		BeanPubContentImage item = mArrayList.get(position);

		if (!TextUtils.isEmpty(item.MediaUri)) {
			// String fileName = EncodeMD5.getMd5(item.MediaUri);
			// String filePath = SdLocal.getCacheImagePath(mContext, fileName);
			
			String url = item.MediaUri;
			String imageUrl = "";
			if(url.startsWith("http://"))
			{
				imageUrl = url;
			}else{
				imageUrl = Setting.getImageUrl(url);
			}

			// int height = UtilityBase.parseInt(item.Height);
			//
			// if(height < 800)
			// {
			// height = 800;
			// }else{
			// height = 1300;
			// }

			int height = DensityUtil.dip2px(mContext, 250);

			holder.itemImageView.getLayoutParams().height = height;

			// BitmapManager.getInstance().loadBitmap(imageUrl, filePath,
			// holder.itemImageView, null, width, height, true);

			// BitmapManager.getInstance().loadBitmap(imageUrl, filePath,
			// holder.itemImageView, null, true);

			String fileName = EncodeMD5.getMd5(imageUrl);
			String filePath = SdLocal.getCacheImagePath(mContext, fileName);

			if (new File(filePath).exists()) {
				UniversalImageLoadTool.disPlay("file://" + filePath,
						holder.itemImageView, R.drawable.default_icon_big);
			} else {
				UniversalImageLoadTool.disPlay(imageUrl, holder.itemImageView,
						R.drawable.default_icon_big);
			}
		}

		holder.itemImageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				String[] urlArray = new String[mArrayList.size()];

				for (int i = 0; i < mArrayList.size(); i++) {
					urlArray[i] = mArrayList.get(i).MediaUri;
				}

				Intent intent = new Intent(mContext,
						AppImageBroswerActivity.class);
				intent.putExtra("isShowTopbar", false);
				intent.putExtra("index", position);
				intent.putExtra("urlArray", urlArray);
				((BaseFragmentActivity) mContext).startActivity(intent);
			}
		});

		return convertView;
	}

	private static class ViewHolder {
		ImageView itemImageView;
	}
}
